<template>
  <div class="xtx-pagination">
      <a @click="changePager(myCurrentPage -1)" href="javascript:;" v-if="myCurrentPage > 1">上一页</a>
      <a href="javascript:;" v-else class="hidden">上一页</a>
      <span v-if="pager.start > 1">···</span>
      <a href="javascript:;" @click="changePager(i)" :class="{active:myCurrentPage === i}" v-for="i in pager.btnArr.value" :key="i">{{i}}</a>
      <span v-if="pager.end <pager.pageCount">···</span>
      <a @click="changePager(myCurrentPage +1)" href="javascript:;" v-if="myCurrentPage < pager.pageCount">下一页</a>
      <a href="javascript:;" v-else class="hidden">下一页</a>

  </div>
</template>

<script>
import { computed, ref, watch } from 'vue'
export default {
  name: 'XtxPagination',
  props: {
    //
    total: {
      type: Number,
      default: 100
    },
    pageSize: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    }
  },
  setup (props, { emit }) {
    //  需要数据:
    // 1.约定按钮个数 5 个,如果成为动态的就设置响应式的
    const count = 5
    // 2.当前显示页码
    const myCurrentPage = ref(1)
    // 3.总页数 = 总条数 /每一页条数  向上取整
    const myTotal = ref(props.total)
    // console.log(myTotal)
    const myPageSize = ref(props.pageSize)
    // console.log(myPageSize.value)
    // 其他数据(总页数,起始按钮，结束按钮，按钮数组) 以来上面数据得到
    const pager = computed(() => {
      // 总页数
      const pageCount = Math.ceil(myTotal.value / myPageSize.value)
      // 起始按钮，结束按钮，按钮数组
      // 1.理想情况下:
      const offset = Math.floor(count / 2)
      let start = myCurrentPage.value - offset
      let end = start + count - 1
      // 2.如果起始页码小于0，需要处理
      if (start < 1) {
        start = 1
        end = (start + count - 1) > pageCount ? pageCount : (start + count - 1)
      }
      //  3.如果结束页码>总页数
      if (end > pageCount) {
        end = pageCount
        start = (end - count + 1) < 1 ? 1 : (end - count + 1)
      }
      const btnArr = ref([])
      for (let i = start; i <= end; i++) {
        btnArr.value.push(i)
      }
      // console.log(btnArr)

      //   提供计算属性数据
      return {
        pageCount,
        btnArr,
        start,
        end

      }
    })
    // 监听props的变化,更新组件内部数据
    // watch(props, () => {
    //   console.log('改变了')
    // })
    // 切换分页
    const changePager = (page) => {
      myCurrentPage.value = page
      // 通知父组件
      emit('currentChange', page)
    }
    watch(props, () => {
      myCurrentPage.value = props.currentPage
    })

    return { myCurrentPage, pager, changePager }
  }

}
</script>

<style scoped lang="less">
.xtx-pagination {
    display: flex;
    justify-content: center;
    padding: 30px;
    border-top: 1px solid #ccc;
    a {
        padding: 5px 10px;
        border: 1px solid #ccc;
        border-radius: 4px;
        margin-right: 8px;
        font-weight: 500;
        color: #333;
        &:hover {
            color: @xtxColor;
        }
        &.hidden {
            opacity: 0.5;
            color: #333;
            cursor: not-allowed;
        }
        &.active {
            background-color: @xtxColor;
            color: #fff;
            border-color: @xtxColor;
        }

    }
    span {
        padding: 5px;
        margin-right: 10px;

    }
}
</style>
